package st.business.tepth.business.taskallocation.modifytrouble;

import android.graphics.Color;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import st.business.tepth.R;
import st.business.tepth.business.bean.TroubleClassificationBean;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;

/**
 * @author zhangyu
 * @date 2019/2/23
 */
public class ModifyTroubleAdapter extends BaseMultiItemQuickAdapter<MultipleItemEntity, BaseViewHolder> {
    public static final int TYPE_0 = 0;
    public static final int TYPE_1 = 1;
    private OnModifyListener mOnModifyListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ModifyTroubleAdapter(List<MultipleItemEntity> data,
                                OnModifyListener modifyListener) {
        super(data);
        addItemType(TYPE_0, R.layout.item_allocation_detail_type_0);
        addItemType(TYPE_1, R.layout.item_allocation_detail_type_1);
        mOnModifyListener = modifyListener;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final MultipleItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_0:
                helper.setText(R.id.tv_classification,
                        (CharSequence) item.getField("name"));
                helper.setTextColor(R.id.tv_classification,
                        Color.parseColor("#ff0000"));
                helper.setText(R.id.tv_other_person, "确定修改");
                helper.getView(R.id.tv_other_person).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<TroubleClassificationBean.ClassBean> classBeans
                                = item.getField("children");
                        int phenomenonId = item.getField("id");
                        int targetSeriesId = item.getField("vehicleSeriesId");
                        if (classBeans != null) {
                            for (TroubleClassificationBean.ClassBean classBean : classBeans) {
                                if (classBean.isSelected()) {
                                    targetSeriesId = classBean.getId();
                                    break;
                                }
                            }
                        }
                        if (mOnModifyListener != null) {
                            mOnModifyListener.onModify(phenomenonId, targetSeriesId);
                        }
                    }
                });
                break;
            case TYPE_1:
                final TroubleClassificationBean.ClassBean classBean =
                        item.getField(ItemFields.ITEM_OBJ);
                helper.setChecked(R.id.check_person, classBean.isSelected());
                helper.setText(R.id.check_person, classBean.getName());
                helper.getView(R.id.layout_checkbox).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isChecked = classBean.isSelected();
                        if (!isChecked) {
                            helper.setChecked(R.id.check_person, true);
                            classBean.setSelected(true);
                            MultipleItemEntity entityHead = item.getField("headEntity");
                            if (entityHead != null) {
                                int position = entityHead.getField("position");
                                if (position != -1) {
                                    MultipleItemEntity oldChild = getItem(position);
                                    if (oldChild != null) {
                                        TroubleClassificationBean.ClassBean oldClassBean =
                                                oldChild.getField(ItemFields.ITEM_OBJ);
                                        oldClassBean.setSelected(false);
                                        notifyItemChanged(position);
                                    }
                                }
                                entityHead.setField("position", helper.getAdapterPosition());
                            }
                        }
                    }
                });
                break;
        }
    }

    public interface OnModifyListener {
        public void onModify(int phenomenonId, int targetSeriesId);
    }
}
